package org.jinion.result.table;

import org.jinion.exception.JinionRuntimeException;
import org.jinion.log.message.result.table.TableRowMessage;
/**
 * @author zhlmmc
 *
 */
public class TableRow {
	/**
	 * This row's number in the ResultSet.
	 */
	private int rowIndex;
	private Object[] values;
	private AbstractTable parentTable;
	
	public TableRow(AbstractTable parentTable){
		this.parentTable = parentTable;
	}
	
	/**
	 * You have to setup columns first!
	 * @param rowNumber start from 1, 2...
	 * @param columnCount
	 */
	public TableRow(AbstractTable parentTable, int rowIndex){
		this(parentTable);
		this.rowIndex = rowIndex;
		values = new Object[parentTable.getColumns().size()];
	}
	
	public int getInt(String columnName){
		return this.getInt(parentTable.getColumnIndex(columnName));
	}
	
	public int getInt(int columnIndex){
		Object obj = values[columnIndex - 1];
		if (obj != null) {
			return Integer.parseInt(obj.toString());
		} else{
			throw new JinionRuntimeException(TableRowMessage.M1.getMessageContent());
		}
	}
	
	public String getString(String columnName){
		return getString(parentTable.getColumnIndex(columnName));
	}
	public String getString(int columnIndex){
		Object obj = values[columnIndex - 1];
		
		if (obj != null) {
			return obj.toString();
		} else{
			return null;
		}
	}
	
	public Object getValue(String columnName){	
		return getValue(parentTable.getColumnIndex(columnName));
	}
	public Object getValue(int columnIndex){
		return values[columnIndex - 1];
	}
	public void setValue(int columnIndex, Object value){
		values[columnIndex - 1] = value;
	}
	public int getRowNumber() {
		return rowIndex;
	}
	public Object[] getValues() {
		return values;
	}
	public void setValues(Object[] values) {
		this.values = values;
	}
	public int getRowIndex() {
		return rowIndex;
	}
	public AbstractTable getParentTable() {
		return parentTable;
	}
}
